package com.aionengine.gameserver.model.templates.item.actions;

import com.aionengine.gameserver.dataholders.DataManager;
import com.aionengine.gameserver.model.DialogAction;
import com.aionengine.gameserver.model.gameobjects.Item;
import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.network.aion.serverpackets.SM_SYSTEM_MESSAGE;
import com.aionengine.gameserver.questEngine.QuestEngine;
import com.aionengine.gameserver.questEngine.model.QuestEnv;
import com.aionengine.gameserver.questEngine.model.QuestState;
import com.aionengine.gameserver.questEngine.model.QuestStatus;
import com.aionengine.gameserver.utils.PacketSendUtility;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;

/**
 * @author Nemiroff Date: 17.12.2009
 */

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "QuestStartAction")
public class QuestStartAction extends AbstractItemAction {

    @XmlAttribute
    protected int questid;

    @Override
    public boolean canAct(Player player, Item parentItem, Item targetItem) {
        QuestState qs = player.getQuestStateList().getQuestState(questid);
        if (qs == null || qs.getStatus() == QuestStatus.NONE || qs.canRepeat())
            return true;

        else if (qs.getStatus() != QuestStatus.COMPLETE)
            PacketSendUtility.sendPacket(player, SM_SYSTEM_MESSAGE.STR_QUEST_ACQUIRE_ERROR_WORKING_QUEST);

        else if (!qs.canRepeat())
            PacketSendUtility.sendPacket(player, SM_SYSTEM_MESSAGE.STR_QUEST_ACQUIRE_ERROR_NONE_REPEATABLE(DataManager.QUEST_DATA.getQuestById(questid).getName()));

        return false;
    }

    @Override
    public void act(Player player, Item parentItem, Item targetItem) {
        QuestEngine.getInstance().onDialog(new QuestEnv(null, player, questid, DialogAction.ASK_QUEST_ACCEPT.id()));
    }
}
